import {ORDER_DURATION, ORDER_DEPART} from './constant'
import {formatTime} from '@/utils/format'

export const SET_FROM = 'SET_FROM'
export const SET_TO = 'SET_TO'
export const SET_DEPART_DATE = 'SET_DEPART_DATE'
export const SET_HIGH_SPEED = 'SET_HIGH_SPEED'
export const SET_TRAIN_LIST = 'SET_TRAIN_LIST'
export const SET_ORDER_TYPE = 'SET_ORDER_TYPE'
export const SET_ONLY_TICKETS = 'SET_ONLY_TICKETS'
export const SET_TICKET_TYPES = 'SET_TICKET_TYPES'
export const SET_CHECKED_TICKET_TYPES = 'SET_CHECKED_TICKET_TYPES'
export const SET_TRAIN_TYPES = 'SET_TRAIN_TYPES'
export const SET_CHECKED_TRAIN_TYPES = 'SET_CHECKED_TRAIN_TYPES'
export const SET_DEPART_STATIONS = 'SET_DEPART_STATIONS'
export const SET_CHECKED_DEPART_STATIONS = 'SET_CHECKED_DEPART_STATIONS'
export const SET_ARRIVE_STATIONS = 'SET_ARRIVE_STATIONS'
export const SET_CHECKED_ARRIVE_STATIONS = 'SET_CHECKED_ARRIVE_STATIONS'
export const SET_DEPART_TIME_START = 'SET_DEPART_TIME_START'
export const SET_DEPART_TIME_END = 'SET_DEPART_TIME_END'
export const SET_ARRIVE_TIME_START = 'SET_ARRIVE_TIME_START'
export const SET_ARRIVE_TIME_END = 'SET_ARRIVE_TIME_END'
export const SET_IS_FILTERS_VISIBLE = 'SET_IS_FILTERS_VISIBLE'
export const SET_SEARCH_PARSED = 'SET_SEARCH_PARSED'

export const setFrom = from =>({type: SET_FROM, data: from})
export const setTo = to => ({type: SET_TO, data: to})
export const setDepartDate = departDate => ({type: SET_DEPART_DATE, data: departDate})
export const setHighSpeed = highSpeed => ({type: SET_HIGH_SPEED, data: highSpeed})
export const setTrainList = trainList => ({type:SET_TRAIN_LIST, data: trainList}) 
export const setTicketTypes = ticketTypes => ({type: SET_TICKET_TYPES, data: ticketTypes})
export const setCheckedTicketTypes = checkedTicketTypes => ({type: SET_CHECKED_TICKET_TYPES, data: checkedTicketTypes})
export const setTrainTypes = trainTypes => ({type: SET_TRAIN_TYPES, data: trainTypes})
export const setCheckedTrainTypes = checkedTrainTypes => ({type: SET_CHECKED_TRAIN_TYPES, data: checkedTrainTypes})
export const setDepartStations = departStations => ({type: SET_DEPART_STATIONS, data: departStations})
export const setCheckedDepartStations = checkedDepartStations => ({type: SET_CHECKED_DEPART_STATIONS, data: checkedDepartStations,})
export const setArriveStations = arriveStations => ({type: SET_ARRIVE_STATIONS, data: arriveStations})
export const setCheckedArriveStations = checkedArriveStations => ({type: SET_CHECKED_ARRIVE_STATIONS, data: checkedArriveStations})
export const setDepartTimeStart = departTimeStart => ({type: SET_DEPART_TIME_START, data: departTimeStart})
export const setDepartTimeEnd = departTimeEnd => ({type: SET_DEPART_TIME_END, data: departTimeEnd})
export const setArriveTimeStart = (arriveTimeStart) => ({type: SET_ARRIVE_TIME_START, data: arriveTimeStart})
export const setArriveTimeEnd = (arriveTimeEnd) => ({type: SET_ARRIVE_TIME_END, data: arriveTimeEnd})
export const setSearchParsed = (searchParsed) => ({type: SET_SEARCH_PARSED, data: searchParsed})
export const toggleHighSpeed = () => {
  return (dispatch, getState) => {
    const {highSpeed} = getState()
    dispatch(setHighSpeed(!highSpeed))
  }
}
export const toggleOrderType = () => {
  return (dispatch, getState) => {
    const {orderType} = getState()
    if (orderType === ORDER_DEPART) dispatch({type: SET_ORDER_TYPE, data: ORDER_DURATION})
    else dispatch({type: SET_ORDER_TYPE, data: ORDER_DEPART})
  }
}
export const toggleOnlyTickets = () => {
  return (dispatch, getState) => {
    const {onlyTickets} = getState()
    dispatch({type: SET_ONLY_TICKETS, data: !onlyTickets})
  }
}
export const toggleIsFiltersVisible = () => {
  return (dispatch, getState) => {
    const {isFiltersVisible} = getState()
    dispatch({ type: SET_IS_FILTERS_VISIBLE, data: !isFiltersVisible})
  }
}
export const nextDate = () => {
  return (dispatch, getState) => {
    const {departDate} = getState()
    dispatch(setDepartDate(formatTime(departDate) + 86400 * 1000))
  }
}
export const prevDate = () => {
  return (dispatch, getState) => {
    const {departDate} = getState()
    dispatch(setDepartDate(formatTime(departDate) - 86400 * 1000))
  }
}
